<script lang="ts" setup name="LoginForm">
import { XtxMessage } from '@/components/message/message';
import userStore from '@/store';
import { ref ,watch} from 'vue';
import { useRouter } from 'vue-router';
import { useField, useForm } from 'vee-validate'
import { useIntervalFn } from '@vueuse/shared';
import { useCount } from '@/utils/hooks';
const { users } = userStore()
const router = useRouter()
const active = ref<'account' | 'mobail'>('account')
const codeRef = ref<HTMLInputElement | null>(null)
const mobailRef = ref<HTMLInputElement | null>(null)
// const isAll = ref(false) 
// const form = ref({
//   account: '',
//   password: '',
//   isAgree: false,
// })
const hLogin = async()=>{
  // if (form.value.account === '') {
  //   XtxMessage({ type: 'error', text: '用户名或手机号不能为空' })
  //   return
  // }
  // if (form.value.password === '') {
  //   XtxMessage({ type: 'error', text: '密码不能为空' })
  //   return
  // }
  // if (!form.value.isAgree) {
  //   XtxMessage({ type: 'error', text: '请同意许可' })
  //   return
  // }
  // console.log('通过校验，可以发送请求')
  //兜底校验
  const res = await validate()
  // if(!res.valid)return
    if(active.value === 'account'){
      if (res.errors.account || res.errors.password || res.errors.isAgree) return
      await users.login({account : account.value,password:password.value})
    }else{
      if (res.errors.mobile || res.errors.code ) return
      await users.mobailLogin({mobile : mobail.value , code:code.value})
    }
      XtxMessage.success('登陆成功')
      router.push('/')
    
}

const { validate,resetForm} = useForm({
  validationSchema:{
    account(value:string){
       if(value?.trim().length === 0) return '用户名不能为空'
       if(!/^[a-zA-Z]\w{5,24}$/.test(value)) return '必须输入以字母开头的字符'
       return true
    },
    password(value : string){
      if(value?.trim().length === 0) return '密码不能为空'
      if(!/^\w{6,18}$/.test(value)) return '必须输入6到18为字符'
      return true
    },
    isAgree(value : boolean){
      if(!value) return '请勾选同意条款' 
      return true
    },
    mobail(value :string){
      if(!value) return '请输入手机号'
      if(!/^1[3,9]\d{9}$/.test(value)) return '请输入正确的手机号码'
      return true
    },
    code(value :string){
      if(!value)return '请输入验证码'
      if(!/^\d{6}$/.test(value)) return '请输入正确的验证码'
      return true
    }
  },
    initialValues: {
    mobile: '13666666666',
    code: '123456',
    account: 'xiaotuxian001',
    password: '123456',
    isAgree: true
  }
})

const { value : account,errorMessage:accountMessage} = useField<string>('account')
const { value : password,errorMessage:passwordMessage} = useField<string>('password')
const { value : isAgree,errorMessage:isAgreeMessage} = useField<boolean>('isAgree')
const { value : mobail,errorMessage:mobailMessage,validate:validateMobail} = useField<string>('mobail')
const { value : code,errorMessage:codeMessage} = useField<string>('code')
watch(active,()=>resetForm())
const { time , start} = useCount(5)
const send = async()=>{

  // const timeId = setInterval(()=>{
  //   time.value--
  //   if(time.value===0){
  //     clearInterval(timeId)
  //   }
  // },1000)
  const res = await validateMobail()
  if(!res.valid)return mobailRef.value?.focus()
  codeRef.value?.focus()
  start()
  await users.getCode(mobail.value)
  XtxMessage.success('发送短息成功')



}
</script>
<template>
  <div class="account-box">
    <div class="toggle">
      <a @click="active = 'account'" href="javascript:;" v-if="active === 'mobail'">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a @click="active = 'mobail'" href="javascript:;" v-else>
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>
    <div class="form">
      <template v-if="active === 'account'">
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <input v-model="account" type="text" placeholder="请输入用户名或手机号" />
          </div>
          <div class="error" v-if="accountMessage"><i class="iconfont icon-warning" />{{ accountMessage }}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <input v-model="password" type="password" placeholder="请输入密码" />
          </div>
          <div class="error" v-if="passwordMessage"><i class="iconfont icon-warning" />{{ passwordMessage }}</div>
        </div>
      </template>
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <input ref="mobailRef" v-model="mobail" type="text" placeholder="请输入手机号" />
          </div>
           <div class="error" v-if="mobailMessage"><i class="iconfont icon-warning" />{{ mobailMessage }}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <input ref="codeRef" v-model="code"  type="password" placeholder="请输入验证码" />
            <span class="code" @click="send">{{time===0?'发送验证码' :`${time}s后重试`}}</span>
          </div>
           <div class="error" v-if="codeMessage"><i class="iconfont icon-warning" />{{ codeMessage }}</div>
        </div>
      </template>
      <div class="form-item">
        <div class="agree">
         
          <XtxCheckbox v-model="isAgree">我已同意</XtxCheckbox>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <div class="error" v-if="isAgreeMessage"><i class="iconfont icon-warning" />{{ isAgreeMessage }}</div>
      </div>
      <a href="javascript:;" class="btn" @click="hLogin">登录</a>
    </div>
    <div class="action">
      <a
        href="https://graph.qq.com/oauth2.0/authorize?client_id=100556005&amp;response_type=token&amp;scope=all&amp;redirect_uri=http%3A%2F%2Fwww.corho.com%3A8080%2F%23%2Flogin%2Fcallback"
        ><img
          src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
          alt="QQ登录"
          border="0"
      /></a>
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      cursor: pointer;
    }
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
